import requests, requests.utils, pickle
from http.cookiejar import LWPCookieJar, Cookie
import crawler.crawler
import time

def printResponse(res, is_json = False):
    """
    Dump a response with more detailed information.
    """
    res.encoding = 'utf-8'
    print('\n'
        'Response from: ' + res.request.url + '\n')
    print('Headers: \n')
    print(res.headers)
    print('\n')
    print('Cookies: \n')
    print(res.cookies)
    print('\n')
    if is_json:
        print('Content:\n' + res.json().__str__()+ '\n')
    else:
        print('Content:\n' + res.text + '\n')

def dumpCookies(cookiejar, path):
    """
    Dump cookies to local storage for next crawling.
    """
    lwp_cookiejar = LWPCookieJar()
    for c in cookiejar:
        args = dict(vars(c).items())
        args['rest'] = args['_rest']
        del args['_rest']
        c = Cookie(**args)
        lwp_cookiejar.set_cookie(c)
    lwp_cookiejar.save(path, ignore_discard=True)

def loadCookies(path):
    """
    Load cookies from local storage.
    """
    lwp_cookiejar = LWPCookieJar()
    lwp_cookiejar.load(path, ignore_discard=True)
    return lwp_cookiejar

def dumpTrendingTopics(trtopic, category):
    """
    Dump trending topics of a specific category to database.
    """
def strToInteger(string):
    if string[-1] == '万':
        return float(string[:-1])*10000
    elif string[-1] == '亿':
        return float(string[:-1])*100000000
    else:
        return float(string)
def WatchRequest(request):
    def handler(*args, **kwargs):
        if crawler.crawler.__DEBUG__:
            print('Requesting: ' + args[1])
        return request(*args, **kwargs)
        if crawler.crawler.__DEBUG__:
            print('Done')
    return handler
def getTimeStamp():
    ts = time.localtime()
    ts_str = str(ts.tm_year)
    if ts.tm_mon < 10:
        ts_str += '0' + str(ts.tm_mon)
    else:
        ts_str += str(ts.tm_mon)
    if ts.tm_mday < 10:
        ts_str += '0' + str(ts.tm_mday)
    else:
        ts_str += str(ts.tm_mday)
    tslot = ts.tm_hour * 6 + ts.tm_min//10
    if tslot < 10:
        ts_str += '00' + str(tslot)
    elif tslot < 100:
        ts_str += '0' + str(tslot)
    else:
        ts_str += str(tslot)
    return ts_str

def dumpTopics(topics):
    """
    Dump the input topics into local database.
    """
    


